package com.technician.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.technician.constant.CommonConstant;
import com.technician.exceptionHandler.GlobalSystemException;
import com.technician.mapper.GoodsTypeMapper;
import com.technician.model.dto.goodsType.GoodsTypeQueryRequest;
import com.technician.model.entity.GoodsType;
import com.technician.service.GoodsTypeService;
import com.technician.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class GoodsTypeServiceImpl extends ServiceImpl<GoodsTypeMapper,GoodsType> implements GoodsTypeService {
    @Override
    public QueryWrapper<GoodsType> getQueryWrapper(GoodsTypeQueryRequest goodsTypeQueryRequest) {
        if (goodsTypeQueryRequest == null) {
            throw new GlobalSystemException(400,"请求参数异常");
        }
        Long id = goodsTypeQueryRequest.getId();
        String name = goodsTypeQueryRequest.getName();
         String sortField = goodsTypeQueryRequest.getSortField();
        String sortOrder = goodsTypeQueryRequest.getSortOrder();

        QueryWrapper<GoodsType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.eq(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }
}
